package cn.com.shopec.erp.stock.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import cn.com.shopec.erp.common.excel.ExcelBaseExport;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.financing.service.IWhareHouseServer;
import cn.com.shopec.erp.stock.excel.impl.StockHistoryExcelExport;
import cn.com.shopec.erp.stock.service.IStockHistoryService;
import cn.com.shopec.erp.stock.vo.StockHistoryVo;

@Controller
@RequestMapping("stockmanage/stockHistoryController")
public class StockHistoryController {

	@Resource
	private IStockHistoryService stockHistoryService;

	@Resource
	private IWhareHouseServer iWhareHouseServer;

	/**
	 * 分页查询出入库历史信息
	 * 
	 * @param packageInfo
	 * @param pageQuery
	 * @param map
	 * @return
	 */
	@RequestMapping("ajaxPageFinderStockHistory")
	@ResponseBody
	public String ajaxPageFinderStockHistory(StockHistoryVo stockHistory, PageQuery pageQuery, ModelMap map) {
		JSONObject jsonObj = new JSONObject();
		PageFinder<StockHistoryVo> pageFinder = stockHistoryService.queryStockHistoryList(stockHistory, pageQuery);
		jsonObj.put("Rows", pageFinder.getData());
		jsonObj.put("Total", pageFinder.getRowCount());
		return jsonObj.toJSONString();
	}

	/**
	 * 跳转到出入库页面
	 * 
	 * @param stockHistory
	 * @param pageQuery
	 * @param map
	 * @return
	 */
	@RequestMapping("pageFinderStockHistory")
	public String pageFinderStockHistory(StockHistoryVo stockHistory, PageQuery pageQuery, ModelMap map) {

		map.put("whareHouse", iWhareHouseServer.getPagedCollectSetList(pageQuery).getData());
		map.put("stockHistory", stockHistory);
		return "/stock/stock_histroy_list";
	}

	/**
	 * 跳转到导出页面
	 * 
	 * @param stockHistory
	 * @param pageQuery
	 * @param map
	 * @return
	 */
	@RequestMapping("toDownloadStockHistory")
	public String toDownloadStockHistory() {
		return "/stock/stock_histroy_download";
	}

	/**
	 * 导出库存历史变更记录
	 * 
	 * @param stockHistory
	 * @param pageQuery
	 * @param map
	 * @return
	 */
	@RequestMapping("downloadStockHistory")
	public void downloadStockHistory(StockHistoryVo stockHistory, HttpServletResponse response) {
		List<StockHistoryVo> datas = stockHistoryService.queryStockHistoryList(stockHistory);
		ExcelBaseExport excelBaseExport = new StockHistoryExcelExport(datas, "库存变更信息", response);
		try {
			excelBaseExport.wirteExcel();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
